using System;
using System.Collections.Generic;
using System.Threading;
using Mbs;

namespace EventBroker.Module
{
    public class CustomerListPresenter : Presenter<ICustomerListView>, ICustomerListPresenter
	{
		private bool processing = false;
		private bool cancelled = false;

        public CustomerListPresenter()
        {
            LocalCustomers = new List<string>();
            GlobalCustomers = new List<string>();
        }

        public List<string> LocalCustomers { get; set; }
        public List<string> GlobalCustomers { get; set; }

        public override IEnumerator<IDisposable> Subscribe()
        {
            yield return CommandNames.AddLocalCustomer.Subscribe<EventArgs>((s, e) => AddLocalCustomer());
            yield return CommandNames.AddGlobleCustomer.Subscribe<EventArgs>((s, e) => AddGlobalCustomer());
            yield return CommandNames.ProcessLocalCustomer.Subscribe<EventArgs>((s, e) => ProcessLocalCustomers());
            yield return CommandNames.CanelProcessLocalCustomer.Subscribe<EventArgs>((s, e) => CancelProcess());
        }

		private void AddLocalCustomer()
		{
            if (!string.IsNullOrEmpty(View.CustomerId))
            {
                LocalCustomers.Add(View.CustomerId);
                View.DisplayLocalCustomers(LocalCustomers);
                View.CustomerId = null;
            }
            View.CustomerIdFocus();
		}
		
		private void AddGlobalCustomer()
		{
           
            if (!string.IsNullOrEmpty(View.CustomerId))
            {
                GlobalCustomers.Add(View.CustomerId);

                CommandNames.GlobalCustomerAdded.Publish(this, GlobalCustomers);
                View.CustomerId = null;
            }
            View.CustomerIdFocus();
		}

		private void ProcessLocalCustomers()
		{
			if (!processing)
			{
				processing = true;
				cancelled = false;

                using (IProgressView progressView = Locator.Get<IProgressView>())
                {
                    CommandNames.CanelProcessLocalCustomer.Command().Enabled = true;

                    List<string> customers = LocalCustomers;
                    for (int i = 0; i < customers.Count; i++)
                    {
                        if (cancelled) break;

                        var customer = customers[i];
                        GlobalCustomers.Add(customer);

                        CommandNames.GlobalCustomerAdded.Publish(this, GlobalCustomers);

                        if (i == 0)
                            progressView.Show();

                        progressView.ProgressValue = (i + 1) * (100 / customers.Count);

                        Thread.Sleep(100);
                    }

                    CommandNames.CanelProcessLocalCustomer.Command().Enabled = false;
                    cancelled = false;
                    processing = false;

                    LocalCustomers.Clear();
                    View.DisplayLocalCustomers(LocalCustomers);
                }
			}
		}

        private void CancelProcess()
        {
            if (processing)
            {
                cancelled = true;
            }
        }

        public void Navigate()
        {
            View.DisplayGlobalCustomers(GlobalCustomers);
            View.Display();
        }
		
	}
}